package top.wshape1.rpc.message.serializer;

import org.nustaq.serialization.FSTConfiguration;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2024-02-04
 */

public class FstSerializer implements Serializer {

    private final FSTConfiguration conf = FSTConfiguration.createDefaultConfiguration();

    @Override
    public <T> byte[] serialize(T object) {
        try {
            return conf.asByteArray(object);
        } catch (Exception e) {
            throw new RuntimeException("Serialization failed: " + e);
        }
    }

    @Override
    public <T> T deserialize(byte[] bytes, Class<T> clazz) {
        try {
            return clazz.cast(conf.asObject(bytes));
        } catch (Exception e) {
            throw new RuntimeException("Deserialization failed: " + e);
        }
    }
}
